package com.fitbet.admin.service.principal.impl;

import java.util.List;

import org.apache.commons.lang3.ClassUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo2do.core.entity.exception.BusinessException;
import com.demo2do.core.entity.page.Page;
import com.demo2do.core.persistence.GenericDaoSupport;
import com.demo2do.core.persistence.annotation.Dao;
import com.demo2do.core.persistence.support.QueryFilter;
import com.fitbet.admin.dao.PrincipalDao;
import com.fitbet.admin.entity.principal.Principal;
import com.fitbet.admin.entity.security.Login;
import com.fitbet.admin.service.impl.DataServiceImpl;
import com.fitbet.admin.service.principal.PrincipalService;

/**
 * @author xiaoHU
 *
 */
@Service("principalService")
public class PrincipalServiceImpl extends DataServiceImpl<Principal> implements PrincipalService {

    @Dao
    private PrincipalDao principalDao;
    
    @Autowired
    private GenericDaoSupport genericDaoSupport;
    
    /* (non-Javadoc)
     * @see com.fitbet.admin.service.PrincipalService#count(com.fitbet.admin.entity.security.Login)
     */
    public int count(Login login) {
        return principalDao.count();
    }
    
    /*
     * (non-Javadoc)
     * @see com.fitbet.admin.service.principal.PrincipalService#load(java.lang.String)
     */
    public Principal load(String name) {
        return principalDao.load(name);
    }

    /* (non-Javadoc)
     * @see com.fitbet.admin.service.PrincipalService#list(com.fitbet.admin.entity.security.Login, com.demo2do.core.entity.page.Page)
     */
    public List<Principal> list(Login login, Page page) {
        List<Principal> principals = principalDao.listByPage(page.getBeginIndex(), page.getMaxResultRecords());
        page.calculate();
        return principals;
    }

    /* (non-Javadoc)
     * @see com.fitbet.admin.service.PrincipalService#createPrincipal(com.fitbet.admin.entity.principal.Principal)
     */
    public void createPrincipal(Principal principal) {
        
     // 1. check create principal
        Principal principal1=principalDao.load(principal.getName());
        
        if(principal1 != null) {
            throw BusinessException.newInstance("400", "新增用户已存在");
        }
        
        // 2. do save principal
        genericDaoSupport.save(principal.initliaze(principal));
        
    }

    /* (non-Javadoc)
     * @see com.fitbet.admin.service.PrincipalService#freeze(com.fitbet.admin.entity.principal.Principal)
     */
    public void freeze(Principal principal) {
        
        principal.freeze();
        genericDaoSupport.update(principal);
        
    }

    /* (non-Javadoc)
     * @see com.fitbet.admin.service.PrincipalService#activate(com.fitbet.admin.entity.principal.Principal)
     */
    public void activate(Principal principal) {
        
        principal.activate();
        genericDaoSupport.update(principal);
        
    }

    /* (non-Javadoc)
     * @see com.fitbet.admin.service.PrincipalService#resetPassword(com.fitbet.admin.entity.principal.Principal)
     */
    public void resetPassword(Principal principal) {
        
        principal.resetPassword();
        genericDaoSupport.update(principal);
        
    }

    /* (non-Javadoc)
     * @see com.fitbet.admin.service.PrincipalService#changePassword(com.fitbet.admin.entity.principal.Principal, java.lang.String)
     */
    public void changePassword(Principal principal, String password) {
        
        genericDaoSupport.update(principal.changePassword(password));
        
    }

    /* (non-Javadoc)
     * @see com.fitbet.admin.service.PrincipalService#edit(java.lang.Long, com.fitbet.admin.entity.principal.Principal)
     */
    public void edit(Long id, Principal principal) {
        
        List<Principal> loads = principalDao.loads(principal.getName());
        Principal load = genericDaoSupport.load(Principal.class, id);
        // 如果名字没有修改，更新
        if (principal.getName().equals(load.getName())) {
            genericDaoSupport.update(load.edit(principal));
        } else if (loads.size() > 0) {
            throw BusinessException.newInstance("400", "名字不可重复！");
        }
        
    }

    /* (non-Javadoc)
     * @see com.fitbet.admin.service.PrincipalService#countSearch(java.lang.Class, com.demo2do.core.persistence.support.QueryFilter)
     */
    public int countSearch(Class<Principal> clazz, QueryFilter queryFilter) {
        
        return genericDaoSupport.searchForInt("SELECT COUNT(*) FROM " + ClassUtils.getShortClassName(clazz) + queryFilter.whereSentence(), queryFilter.parameters());
        
    }

}
